<?php
namespace Home\Controller;
use Home\Model\UsersModel;
use Think\Controller;


class LoginController extends Controller {
    public function login(){
        if (IS_POST){
            $userdata=array('status'=>false,'info'=>'登录失败！','url'=>C('BASE_URL').'/Login/login');
            //判断用户合法性

            if(empty($_POST['username']) || empty($_POST['pwd']))
            {
                $userdata=array('status'=>false,'info'=>'登录失败：用户名或密码不能为空！','url'=>C('BASE_URL').'/Login/login');
                $this->ajaxReturn($userdata,'JSON');
                return;
            }

            $userinfo=new UsersModel();
            $userret=$userinfo->getUserData($_POST['username'],md5($_POST["pwd"]));
            if(!$userret)
            {
                $userdata=array('status'=>false,'info'=>'登录失败：用户不存在或密码错误！','url'=>C('BASE_URL').'/Login/login');
            }
            else
            {
                //记录登录日志
                $userdata=array('status'=>true,'info'=>'登录成功！','url'=>C('BASE_URL').'/Index/index');

                $_SESSION['adminuser'] = $userret[0];
            }

            $this->ajaxReturn($userdata,'JSON');
        }else{
            $this->display('login');
        }
    }

    public function loginout()
    {
        session(null);
        header ( "Location: ".C("BASE_URL")."/Login/login" );
        exit ();
    }

}